package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.TestVo.ApiMenuItem;
import com.cskaoyan.market.TestVo.MenuItem;
import com.cskaoyan.market.TestVo.MidMenuItem;
import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketRoleMapper;
import com.cskaoyan.market.service.admin.MarketAdminService;
import com.cskaoyan.market.service.admin.MarketPermissionService;
import com.cskaoyan.market.service.admin.MarketRoleService;
import com.cskaoyan.market.util.Permission;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.util.ScanResults;
import com.cskaoyan.market.vo.MarketRoleVo;
import com.cskaoyan.market.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/admin/role/")
public class AdminRoleController extends HttpServlet {
    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Autowired
    MarketRoleService marketRoleService;

    @Autowired
    MarketPermissionService marketPermissionService;
    @Autowired
    ScanResults scanResults;
    @Autowired
    MarketAdminService marketAdminService;
    @GetMapping("options")
    public Object options()  {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        List<MarketRoleVo> marketRoleVoList = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            marketRoleVoList.add(new MarketRoleVo(marketRole.getName(),marketRole.getId()));
        }
        return ResponseUtil.okList(marketRoleVoList);
    }


    @GetMapping("/list")
    //page=1&limit=20&name=%E8%90%A8%E7%93%A6%E8%90%A8%E7%93%A6&sort=add_time&order=desc
    public Object list(String page,String limit,String name,String sort,String order) {
        Integer limitInt = null;
        Integer pageInt = null;
        try {
            limitInt = Integer.parseInt(limit);
            pageInt = Integer.parseInt(page);
        } catch (Exception e) {
            //直接返回一个响应结果
            System.out.println("e = " + e);
            return ResponseUtil.badArgument();
        }try {
            List<MarketRole> marketRoleList = marketRoleService.list(pageInt, limitInt, sort, order, name);
            return ResponseUtil.okList(marketRoleList);
        }catch (Exception e) {
            return ResponseUtil.fail(500,"服务器内部错误");
        }
    }
    @PostMapping("update")
    public Object update(MarketRole marketRole) {
        if (marketRole == null || marketRole.getId() == null) {
            return ResponseUtil.fail(404, "id不能为空");
        }try {
            marketRoleService.update(marketRole);
            return ResponseUtil.ok();
        }catch (Exception e){
            System.out.println("e = " + e);
            return ResponseUtil.fail(405,"update failed");
        }
    }

    @PostMapping("delete")
    public Object delete(MarketRole marketRole) {
        if (marketRole == null || marketRole.getId() == null) {
            return ResponseUtil.fail(404, "id不能为空");
        }try {
            marketRoleService.delete(marketRole);
            return ResponseUtil.ok();
        }catch (Exception e){
            System.out.println("e = " + e);
            return ResponseUtil.fail(405,"delete failed");
        }
    }

    @PostMapping("/permissions")
    private Object permissions(@RequestBody Map<String,Object> map) {
        String roleId = (String) map.get("roleId");
        List<String> permissions = (List<String>)map.get("permissions");
        if (roleId == null || permissions == null || permissions.size() == 0) {
            return ResponseUtil.fail(404, "roleId和permissions不能为空");
        }else {
            Boolean flag = marketPermissionService.updatePermissions(roleId, permissions);
            if (flag) {
                return ResponseUtil.ok();
            }else {
                return ResponseUtil.fail(405, "update failed");
            }
        }
    }




    @GetMapping("/permissions")
    public Object permissionsGet(String roleId) {
        if (roleId == null) {
            return ResponseUtil.fail(404, "roleId不能为空");
        }else {
            MarketAdmin marketAdmin = marketAdminService.selectById(roleId);
            Integer[] roleIds = marketAdmin.getRoleIds();
            Map<String, List<String>> permissionsAndRoles = marketPermissionService.getPermissionsAndRoles(roleIds);
            Map<String, Object> data = new HashMap<>();
            data.put("assignedPermissions", permissionsAndRoles.get("perms"));

            //TODO
        }
        return ResponseUtil.fail();
    }

}
